package com.kaifamiao.base.exam20210925;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author lucien-ma
 * @date 2021/9/25
 * @apiNote
 */
public class Demo02 {

    public static void main(String[] args) {
        int n = split(new File("E:/自学资料/java/黑马视频/day1/001_继承.mp4"),
                1024 * 1024, new File("E:/copy"));
        System.out.println(n);

        long size = join(new File("E:/copy"), new File("E:/test/join.mp4"), ".temp");
        System.out.println(size);
    }

    /**
     * 拆分文件
     * @param original 要拆分的文件
     * @param size     拆分的大小
     * @param directory 拆分后存放的路径
     * @return  返回拆分的数量
     */
    public static int split(final File original, final long size, final File directory) {
        if (!original.exists())
            return -1;
        if (!directory.exists()) {
            directory.mkdirs();
        }
        // 表示拆分后文件的数量
        int n = (int) (original.length() / size);
        if (original.length() % size != 0) {
            n++;
        }
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            byte[] bytes = new byte[(int) size];
            // 输入流
            inputStream = new FileInputStream(original);
            for (int i = 1; i <= n; i++) {
                outputStream = new FileOutputStream(new File(directory,"video-"+i+".temp"));
                int c = -1;
                c = inputStream.read(bytes);
                outputStream.write(bytes, 0, c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return n;
    }

    /**
     * 合并文件
     * @param dir  要合并文件所存放的目录
     * @param target 合并后的文件路径
     * @param suffix 要合并文件的后缀 (.mp4, .txt)
     * @return 返回合并后文件的大小
     */
    public static long join(final File dir, final File target, final String suffix) {
        if (!dir.exists())
            return -1;
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(target, true);
            byte[] bytes = new byte[1204 * 1024];
            // 获取要合并文件的 File 对象
            File[] files = dir.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(suffix);
                }
            });
            Arrays.sort(files, new Comparator<File>() {
                @Override
                public int compare(File o1, File o2) {
                    return o1.getName().hashCode() - o2.getName().hashCode();
                }
            });
            for (File file : files) {
                System.out.println(file);
                inputStream = new FileInputStream(file);
                int c = -1;
                while ((c = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, c);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return target.length();
    }
}
